home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SPACE 2
/
SPACE - Library 2 - Volume 1.iso
/
utility
/
647
/
pcraw2st
/
pcraw2st.c
next >
Wrap
C/C++ Source or Header
|
1992-10-25
|
10KB
|
293 lines
/**
: pcraw2st.c
: Chris Herborth, Sept.8/92
: compile with -DDEBUG to enable DEBUGging output
: compile with -DUSE_FPRINTF to use fprintf() instead of fputc()
(I dunno why you'd want to do that, but you can...)
: compile with -DINTEL on an Intel-based machine (ie, DOS or any
other 80x86 computer)
: This is to convert PC RAW files, produced by ray tracers such as QRT,
DKB, and POV-Ray, into ST format RAW files. I'm not sure why the guy
who ported QRT to the ST decided to munge their format, but...
: Usage: pcraw2st infile.raw outfile.raw
: Version History
0.0 - First version, didn't work.
0.1 - Worked... kinda.
0.2 - Fixed for Intel/Motorola byte-order difference. Much better!
0.3 - Made the bytes "unsigned char" instead of "char"...
Made the error codes positive instead of negative (thus
preventing some apps/shells from hurling).
Added a bit of sanity checking to the image size.
Added a bit of DEBUGging output.
Fixed a really dumb error (I used a variable as an array
index without initializing it... duh).
0.4 - v 0.3 wouldn't convert 320x200 files (whoops!)
Added much more DEBUGgin output.
Used fputc() instead of fprintf().
Added #include <stdlib.h> for MiNT.
Used calloc() instead of malloc().
Added #ifdef's so you can compile easier (?) on an Intel-based
CPU.
**/
/** : Header files... **/
#include <stdio.h>
#include <string.h>
#ifdef SOZOBON
#ifndef __MINT__
#include <malloc.h>
#else /* __MINT__ */
#include <stdlib.h>
#endif /* __MINT__ */
#endif /* SOZOBON */
/** : Define error messages... **/
#define OK 0 /* No errors. */
#define USAGE_ERR 1 /* Dumb user error. */
#define FILE_SAME 2 /* infile == outfile error */
#define NO_MEMORY 3 /* Insufficient memory */
#define WEIRDSIZE 4 /* Absurd image size */
int main( argc, argv )
unsigned argc;
char **argv;
{
FILE *infile, /* Input file handle. */
*outfile; /* Output file handle. */
char *r_line, /* RGB lines from the PC RAW file */
*g_line, /* malloc() these */
*b_line,
*in_name, /* Filename pointers */
*out_name;
unsigned char thing; /* One input byte */
int width, /* Image width */
height, /* Image height */
line, /* Line loop */
loop, /* Pixel loop */
err_mess, /* Error number */
ret; /* Function return value. */
union
{
int w;
unsigned char b[1];
} spam;
#ifdef DEBUG
printf( "DEBUG (%d): Initializing variables\n", __LINE__ );
#endif
/* Initialize our variables... */
infile = NULL; /* File pointers don't point yet... */
outfile = NULL;
r_line = NULL; /* Neither do our line pointers... */
g_line = NULL;
b_line = NULL;
loop = 1; /* Start with the first argument... */
err_mess = OK; /* We haven't had any errors yet... */
#ifdef DEBUG
printf( "DEBUG (%d): Parsing command line (argc = %d)\n", __LINE__, argc );
#endif
/* Parse the command line... This is mostly here for future expansion... */
if( argc < 2 )
err_mess = USAGE_ERR;
else
{
if( argv[loop][0] == '-' )
switch( argv[loop][1] )
{
case '?':
case 'h':
case 'H':
err_mess = USAGE_ERR;
break;
case 'V':
puts( "pcraw2st 0.4" );
err_mess = 13;
break;
default:
err_mess = USAGE_ERR;
break;
}
else
{
in_name = argv[1];
out_name = argv[2];
ret = strcmp( in_name, out_name );
if( ret == 0 )
err_mess = FILE_SAME;
}
}
/* If we haven't had any problems, try to process the files... */
if( err_mess == OK )
{
#ifdef DEBUG
printf( "DEBUG (%d): Attempting to open input file \"%s\".\n", __LINE__, in_name );
#endif
infile = fopen( in_name, "rb" );
if( infile == NULL )
{
printf( "Unable to open %s...\n", infile );
exit( -1 );
}
#ifdef DEBUG
printf( "DEBUG (%d): Attempting to open output file \"%s\".\n", __LINE__, out_name );
#endif
outfile = fopen( out_name, "wb" );
if( outfile == NULL )
{
printf( "Unable to create %s...\n", outfile );
exit( -1 );
}
#ifdef DEBUG
printf( "DEBUG (%d): Getting image width and height.\n", __LINE__ );
#endif
/* Get our image width... */
/* These have to be swapped around for Intel Idiotic format. */
#ifndef INTEL
spam.b[1] = fgetc( infile ); /* Motorola-based machines use this. */
spam.b[0] = fgetc( infile );
#else
spam.b[0] = fgetc( infile ); /* Intel-based use this. */
spam.b[1] = fgetc( infile );
#endif
width = spam.w;
/* Get our image height... */
/* These have to be swapped around for Intel Idiotic format. */
#ifndef INTEL
spam.b[1] = fgetc( infile ); /* Motorola-based machines use this. */
spam.b[0] = fgetc( infile );
#else
spam.b[0] = fgetc( infile ); /* Intel-based use this. */
spam.b[1] = fgetc( infile );
#endif
height = spam.w;
if( width < 1 || height < 1 )
err_mess = WEIRDSIZE;
if( err_mess == OK )
{
/* Talk to the user... */
printf( "Converting %d x %d image:\n", width, height );
/* Put the width/height into the new file... */
fprintf( outfile, "%d %d%c", width, height, 0x0a );
fflush( outfile );
#ifdef DEBUG
printf( "DEBUG (%d): Allocating line buffers.\n", __LINE__ );
#endif
r_line = calloc( 1, width );
g_line = calloc( 1, width );
b_line = calloc( 1, width );
if( r_line == NULL || g_line == NULL || b_line == NULL )
{
#ifdef DEBUG
printf( "DEBUG (%d): Unable to allocate line buffers.\n", __LINE__ );
#endif
err_mess = NO_MEMORY;
}
if( err_mess == OK )
{
for( line = 0; line < height; line++ )
{
/* Get the current line number. We don't do anything with it. */
/* These have to be swapped around for Intel Idiotic format. */
#ifdef DEBUG
printf( "DEBUG (%d): Getting line %d.\n", __LINE__, line );
#endif
spam.b[1] = fgetc( infile );
spam.b[0] = fgetc( infile ); /* Ignore the line number */
fread( r_line, width, 1, infile );
fread( g_line, width, 1, infile );
fread( b_line, width, 1, infile );
#ifdef DEBUG
printf( "DEBUG (%d): Writing line %d.\n", __LINE__, line );
#endif
for( loop = 0; loop < width; loop++ )
{
#ifndef USE_FPRINTF
fputc( r_line[loop], outfile );
fputc( g_line[loop], outfile );
fputc( b_line[loop], outfile );
#else
fprintf( outfile, "%c%c%c", r_line[loop], g_line[loop],
b_line[loop] );
#endif
}
fflush( outfile );
printf( "." );
if( ( line + 1 ) % 50 == 0 )
printf( "%d", line + 1 );
fflush( stdout );
}
}
}
}
switch( err_mess )
{
case OK:
puts( " done." );
break;
case USAGE_ERR:
puts( "Usage: pcraw2st infile.raw outfile.raw" );
break;
case FILE_SAME:
printf( "Cannot convert %s to %s!\n", in_name, out_name );
puts( "File names must be different!" );
break;
case NO_MEMORY:
puts( "Insufficient memory!" );
break;
case WEIRDSIZE:
printf( "Absurd image size! (%d x %d)\n", width, height );
break;
default:
break;
}
/* Close our files... */
if( infile != NULL )
fclose( infile );
if( outfile != NULL )
fclose( outfile );
/* Toss our memory... */
if( r_line != NULL )
free( r_line );
if( g_line != NULL )
free( g_line );
if( b_line != NULL )
free( b_line );
/* Beat it... */
exit( err_mess );
}